<template>
  <el-dialog
    :title="!dataForm.id ? $t('Common.Add') : $t('Common.Edit')"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="auto"
    >
      <el-form-item label="订单号" prop="orderSn">
        <el-input v-model="dataForm.orderSn" readonly placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-input v-model="orderStatus[dataForm.orderStatus]" readonly placeholder="订单状态"></el-input>
      </el-form-item>
      <el-form-item label="物流单号" prop="expressnum">
        <el-input v-model="dataForm.expressnum" placeholder="物流单号"></el-input>
      </el-form-item>
      <el-form-item label="收货人" prop="consignee">
        <el-input v-model="dataForm.address.consignee" placeholder="收货人"></el-input>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address.address" placeholder="地址"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="dataForm.address.mobile" placeholder="手机"></el-input>
      </el-form-item>
      <el-form-item label="实付金额" prop="amount">
        <el-input v-model="dataForm.amount" :readonly="(dataForm.orderStatus*1 === 12 || dataForm.orderStatus === 0) ? false:'readonly'" placeholder="订单总价"></el-input>
      </el-form-item>
      <el-form-item label="下单时间" prop="submitTime">
        <el-input v-model="dataForm.submitTime" readonly placeholder="下单时间"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('Common.Cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ $t('Common.Confirm') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      orderStatus: {
        0: "待付款",
        12: "待付款",
        1: "已支付",
        2: "待发货",
        3: "待收货",
        4: "待评价",
        5: "已完成",
        13: "退款中",
        14: "已退款",
        15: "已取消",
        16: "错误订单"
      },
      dataForm: {
        id: 0,
        orderSn: "",
        orderSnSubmit: "",
        uid: "",
        orderStatus: "",
        expressnum: "",
        consignee: "",
        address: "",
        mobile: "",
        totalCost: "",
        amount: "",
        submitTime: ""
      },
      dataRule: {
        orderSn: [
          {
            required: true,
            message: "订单号，小程序中显示用不能为空",
            trigger: "blur"
          }
        ],
        uid: [{ required: true, message: "用户id不能为空", trigger: "blur" }],
        orderStatus: [
          {
            required: true,
            message: "订单状态 待付款0 已完成1 待发货2 待收货3 已取消4不能为空",
            trigger: "blur"
          }
        ],
        amount: [
          { required: true, message: "订单总价不能为空", trigger: "blur" }
        ],
        submitTime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/admin/order/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.orderSn = data.data.orderSn;
              this.dataForm.orderSnSubmit = data.data.orderSnSubmit;
              this.dataForm.uid = data.data.uid;
              this.dataForm.orderStatus = data.data.orderStatus;
              this.dataForm.expressnum = data.data.expressnum;
              this.dataForm.consignee = data.data.consignee;
              this.dataForm.addressId = data.data.address.id;
              this.dataForm.address = data.data.address;
              this.dataForm.mobile = data.data.mobile;
              this.dataForm.amount = data.data.amount;
              this.dataForm.submitTime = data.data.submitTime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/admin/order/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              orderSn: this.dataForm.orderSn,
              orderSnSubmit: this.dataForm.orderSnSubmit,
              uid: this.dataForm.uid,
              orderStatus: this.dataForm.orderStatus,
              expressnum: this.dataForm.expressnum,
              consignee: this.dataForm.consignee,
              address: this.dataForm.address.address,
              mobile: this.dataForm.mobile,
              amount: this.dataForm.amount,
              submitTime: this.dataForm.submitTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
